<?php
namespace app\zhihui\controller;

use app\common\model\ZhcheckinMess as ZhcheckinMessModel;
use app\common\model\Company as CompanyModel;
use app\home\controller\Base;
use app\common\model\Member as MemberModel;

/**
 * 入住控制器
 * Class Zhcheckinmess
 * @package app\index\controller
 */
class Zhcheckinmess extends Base
{
    /**
     * [index  列表]
     * @return json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $model = new ZhcheckinMessModel;
        $mmodel = new MemberModel;
        $this->getPageAndSize($param);
        $user = $this->user;
        try{
            if(empty($param['type'])){
                $map['mid'] = $user['id'];
            }
            if(!empty($param['date'])){
                $map['create_time'] = ['between',[strtotime($param['date']),strtotime($param['date'])+24*3600]];
            }
            if(!empty($param['city'])){
                $map['company_city'] = $param['city'];
            }
            if(!empty($param['is_ascetic'])){
                $map['is_ascetic'] = $param['is_ascetic'];
            }
            $map['comid'] = $param['comid'];
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlist($map, $this->from, $this->size,'id,mid,is_ascetic,name,fahao,create_time,tel,idcard,reside_day,sex,company_city');
            foreach($lists as &$v){
                $member = $mmodel->findData($v['mid'],'id','img');
                $http = substr($member['img'],0,4);
                if($http != 'http'){
                    $v['img'] = config('domain_name').$member['img'];
                }else{
                    $v['img'] = $member['img'];
                }
                $v['endtime'] = date('Y-m-d H:i:s',strtotime($v['create_time'])+$v['reside_day']*24*3600);
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '获取成功', [
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [index  列表]
     * @return json
     * @author [C。]
     */
    public function add()
    {
        $param = input('param.');
        $model = new ZhcheckinMessModel;
        $user = $this->user;
        try{
            if(!is_idcard($param['idcard'])){
                return admin_json(-1, '身份证号错误！');
            }
            if(!isMobile($param['tel'])){
                return admin_json(-1, '手机号错误！');
            }
            $param['mid'] = $user['id'];
            $id = $model->addData($param);
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '提交成功',$id);
    }
    /**
     * [getone  获取单挑入住记录]
     * @return json
     * @author [C。]
     */
    public function getone()
    {
        $param = input('param.');
        $model = new ZhcheckinMessModel;
        $cmodel = new CompanyModel;
        $user = $this->user;
        try{
            $company = $cmodel->findData($param['comid'],'id','compic,class_phone,addr');
            $http = substr($company['compic'],0,4);
            if($http != 'http'){
                $company['compic'] = config('domain_name').$company['compic'];
            }

            if(!empty($param['id'])){
                $data = $model->getFindOne(['id'=>$param['id']]);
                $data['compic'] = $company['compic'];
                $data['class_phone'] = $company['class_phone'];
                $data['addr'] = $company['addr'];

                $data['ruzhu_time'] = date('Y-m-d',strtotime($data['create_time']));
                $data['leave_time'] = date('Y-m-d',strtotime($data['ruzhu_time'])+$data['reside_day']*24*3600);
            }else{
                $data = $company;
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '获取成功',$data);
    }
    /**
     * [output  挂单记录导出]
     * @return json
     * @author [C。]
     */
    public function output()
    {
        $param = input('param.');
        $model = new ZhcheckinMessModel;
        $map = [];
        try{
            $map['comid'] = $param['comid'];
            if(!empty($param['date'])){
                $map['create_time'] = ['between',[strtotime($param['date']),strtotime($param['date'])+24*3600]];
            }
            if(!empty($param['city'])){
                $map['company_city'] = $param['city'];
            }
            if(!empty($param['is_ascetic'])){
                $map['is_ascetic'] = $param['is_ascetic'];
            }
            $data = $model->getAllData($map,'id','id,name,sex,tel,is_ascetic,idcard,create_time,reside_day');
            vendor("PHPExcel.PHPExcel");
            $excel = new \PHPExcel();
            //Excel表格式,这里简略写了4列
            $letter = array('A','B','C','D','E','F','G','H');
            //表头数组
            $tableheader = array('ID','姓名','性别','手机号','是否出家','身份证号','创建时间','入住天数');
            //填充表头信息
            for($i = 0;$i < count($tableheader);$i++) {
                $excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
            }

            $excel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
            $excel->getActiveSheet()->getColumnDimension('B')->setWidth(10);
            $excel->getActiveSheet()->getColumnDimension('C')->setWidth(10);
            $excel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
            $excel->getActiveSheet()->getColumnDimension('E')->setWidth(10);
            $excel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
            $excel->getActiveSheet()->getColumnDimension('G')->setWidth(15);
            $excel->getActiveSheet()->getColumnDimension('H')->setWidth(10);
            //填充表格信息
            for ($i = 2;$i <= count($data) + 1;$i++) {
                $j = 0;
                foreach ($data[$i - 2] as $key=>$value) {
                    if($key == 'sex'){
                        if($value == 1){
                            $value = '男';
                        }elseif($value == 2){
                            $value = '女';
                        }
                    }
                    if($key == 'idcard'){
                        $value = ' '.$value.' ';
                    }
                    if($key == 'is_ascetic'){
                        if($value == 1){
                            $value = '已出家';
                        }elseif($value == 2){
                            $value = '未出家';
                        }
                    }
                    $excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value");
                    $j++;
                }
            }

            //创建Excel输入对象
            $write = new \PHPExcel_Writer_Excel5($excel);
//            header("Pragma: public");
//            header("Expires: 0");
//            header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
//            header("Content-Type:application/force-download");
//            header("Content-Type:application/vnd.ms-execl");
//            header("Content-Type:application/octet-stream");
//            header("Content-Type:application/download");;
//            header('Content-type: application/xls');
//            header('Content-Type: application/octet-stream;charset=utf-8');
//            header('Content-Disposition:attachment;filename="挂单记录.xls"');
//            header("Content-Transfer-Encoding:binary");
            $uploaddir = "excel/";//设置文件保存目录 注意包含
            if (!file_exists($uploaddir)) {
                mkdir($uploaddir, 0755, true);
            }
            $filename = $param['token'].'.xls';
            $write->save('./excel/'.$filename);
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '获取成功',$_SERVER['SERVER_NAME'].'/excel/'.$filename);
    }
    /**
     * [checkmessqrcode  生成挂单二维码]
     * @return json
     * @author [C。]
     */
    public function checkmessqrcode()
    {
        $param = input('param.');
        try{
            create_qrcode('https://sy.zhsaas.com/#/wxlogin?id='.$param['comid'].'&url=guadanSub',$param['token'].'.png','logo.png');
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '获取成功',$_SERVER['SERVER_NAME'].'/qrcode/'.$param['token'].'.png');
    }
}